{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第一章 基础知识"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#1.算法：就是流程，由一系列必须按照顺序执行的操作\n",
    "#除法运算结果为浮点数\n",
    "1/1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 双斜杠实现整除\n",
    "1//1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.0"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#浮点数取整还是浮点数\n",
    "5.0//2.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "10//3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-4"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''除法运算 向下取整'''\n",
    "-10//3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#求余\n",
    "10 % 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#求幂\n",
    "3**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-9"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "-3**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pow(3,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pow(-3,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-9"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "-pow(3,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#绝对值\n",
    "abs(-5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#圆整\n",
    "round(4.4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''遇到.5 舍入到偶数'''\n",
    "round(4.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(5.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(4.6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-5"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(-4.6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''int 向下取整'''\n",
    "int(4.9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 向下取整\n",
    "math.floor(4.9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 向上取整\n",
    "math.ceil(4.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.0"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#开方 结果是浮点数\n",
    "math.sqrt(9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1j"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 复数\n",
    "import cmath\n",
    "cmath.sqrt(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 海龟\n",
    "%matplotlib inline\n",
    "from turtle import *\n",
    "forward(100)\n",
    "left(120)\n",
    "forward(100)\n",
    "left(120)\n",
    "forward(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'helloname!'"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字符串拼接\n",
    "'hello' + 'name' + '!'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'helloname'"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'hello''name'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"Let's go\""
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"Let's go\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"Let's go\""
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 饭斜杠 转意\n",
    "'Let\\'s go'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'hello \\n word'\n"
     ]
    }
   ],
   "source": [
    "# repr原格式\n",
    "print(repr('hello \\n word'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello \n",
      " word\n"
     ]
    }
   ],
   "source": [
    "print('hello \\n word')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello word\n"
     ]
    }
   ],
   "source": [
    "# 换行\n",
    "print('hello \\\n",
    "word')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "c:\\nowhree\n"
     ]
    }
   ],
   "source": [
    "#原始字符 r\n",
    "print(r'c:\\nowhree')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "EOL while scanning string literal (<ipython-input-63-de44a121270a>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-63-de44a121270a>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m    print(r'c:\\oooo\\')\u001b[0m\n\u001b[0m                      ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m EOL while scanning string literal\n"
     ]
    }
   ],
   "source": [
    "# 原始字符不能以 \\ 为结尾\n",
    "print(r'c:\\oooo\\')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第二章  列表和元祖"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据结构：是以某种方式结合起来的数据元素集合。\n",
    "# 序列：编号对应数据 如 列表，元组，字符串。\n",
    "# python支持一种数据结构叫做容器：容器是可以包含其他对象的对象。如序列（列表，元组）和映射（字典）。\n",
    "# 序列的基本操作 索引，切片，相加，相乘 和成员资格(in)，长度（len），最小值（min），最大值（max）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "list index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-21-c5440e04a458>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0mnumbers\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m6\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m7\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m8\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m9\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0;31m# 索引时候超过长度会报错\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mnumbers\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m: list index out of range"
     ]
    }
   ],
   "source": [
    "numbers = [1,2,3,4,5,6,7,8,9,10]\n",
    "# 索引时候超过长度会报错\n",
    "numbers[10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9, 10]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切片时候超过长度 不会报错\n",
    "numbers[8:14]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 默认步长是1 从左向右 所以不会切到数据\n",
    "numbers[-3:0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[8, 7]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numbers[-3:-5:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "can only concatenate list (not \"str\") to list",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-12-f4b7ce81e518>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# 序列相加\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m'123'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: can only concatenate list (not \"str\") to list"
     ]
    }
   ],
   "source": [
    "# 序列相加 必须是相同类型的\n",
    "[1,2,3] + '123'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'pythonpythonpythonpythonpython'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 序列相乘\n",
    "'python'*5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[42, 42, 42, 42, 42]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[42]*5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[100, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列表赋值\n",
    "numbers[0] = 100\n",
    "numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[6, 7, 8, 9, 10]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除元素  无返回值\n",
    "del numbers[0:5]\n",
    "numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 列表的方法\n",
    "numbers.append(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[6, 7, 8, 9, 10, 4, [4, 5]]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# append() 添加\n",
    "numbers.append([4,5])\n",
    "numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3]\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "# clear() 清除\n",
    "lis = [1,2,3]\n",
    "print(lis)\n",
    "lis.clear()\n",
    "print(lis)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3] []\n"
     ]
    }
   ],
   "source": [
    "# copy（）\n",
    "a = [1,2,3]\n",
    "b = a.copy()\n",
    "b.clear()\n",
    "print(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 3, 4, 5, [7, 8, 9]]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# extend() 扩展\n",
    "a.extend([3,4,5,[7,8,9]])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# index 索引\n",
    "knights = ['we','are','the','knights','who','say','ni']\n",
    "knights.index('we')\n",
    "#knights.index('we555')  报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[6, 7, 8, 'four', 'four', 9, 10, 4, [4, 5]]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# insert 插入\n",
    "numbers.insert(3,'four')\n",
    "numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4, 5]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pop 弹出 唯一一个修改列表还能返回值的方法\n",
    "numbers.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[6, 7, 8, 'four', 9, 10, 4]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# remove 删除第一如何要求的元素\n",
    "numbers.remove('four')\n",
    "numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4, 10, 9, 'four', 8, 7, 6]"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# reverse 反向排序\n",
    "numbers.reverse()\n",
    "numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'<' not supported between instances of 'str' and 'int'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-37-e1a21f63015e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# sort 排序\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mnumbers\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msort\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      3\u001b[0m \u001b[0mnumbers\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mTypeError\u001b[0m: '<' not supported between instances of 'str' and 'int'"
     ]
    }
   ],
   "source": [
    "# sort 排序 \n",
    "'''列表内元素应为同种类型'''\n",
    "numbers.sort()\n",
    "numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 6, 7]"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lis2 = [1,2,4,6,3,7]\n",
    "lis2.sort()\n",
    "lis2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['h', 'n', 'o', 'p', 't', 'y']"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''注意'''\n",
    "sorted('python')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['we', 'ni', 'are', 'the', 'who', 'say', 'knights']\n",
      "['knights', 'are', 'the', 'who', 'say', 'we', 'ni']\n"
     ]
    }
   ],
   "source": [
    "# 排序可以选择关键词语  key reverse\n",
    "knights.sort(key=len)\n",
    "print(knights)\n",
    "knights.sort(key=len,reverse=True)\n",
    "print(knights)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(42,)"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 元组 不能修改的列表\n",
    "42,"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('p', 'y', 't', 'h', 'o', 'n')"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tuple('python')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第三章 使用字符串 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字符串是不可变的。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串格式化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hello , world. Hot enough for ya?'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字符串格式化。\n",
    "formats = 'Hello , %s. %s enough for ya?'\n",
    "values = ('world','Hot')\n",
    "formats % values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hello, Mars!, Dusty enought for ya?'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''模板字符串'''\n",
    "from string import Template\n",
    "tmpl = Template('Hello, $who!, $what enought for ya?')\n",
    "# 多了test不会报错  少了who和what 会报错\n",
    "tmpl.substitute(who='Mars',what='Dusty',test='f')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'first,second and thire'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 新版本字符串格式化{}\n",
    "\"{},{} and {}\".format('first','second','thire')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'first,thire and second'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 新版本字符串格式化{} 位置参数\n",
    "\"{0},{2} and {1}\".format('first','second','thire')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'to be or not to be'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 新版本字符串格式化{} 位置参数2\n",
    "\"{0} {1} {2} {3} {0} {1}\".format('to','be','or','not')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'pai is approximately 3.14.'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 关键字参数\n",
    "from math import pi\n",
    "\"{name} is approximately {value:.2f}.\".format(value=pi,name='pai')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"Euler's canstant is roughly 2.718281828459045\""
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''关键字参数2 简写'''\n",
    "from math import e\n",
    "f\"Euler's canstant is roughly {e}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"{ceci n'est pas une replacemetn field}\""
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 包含{}\n",
    "\"{{ceci n'est pas une replacemetn field}}\".format()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# {\n",
    "# 字段名（索引/标识符）\n",
    "# 转换标志（！[r(repr),s(str),a(ascii)]）\n",
    "# 格式说明：(包括格式类型（字符串，浮点，十六进制等），字符宽度精度，如何显示千分位，对其和填充方式)}\n",
    "# 抓换说明 ：[填充] [对其方式：=,< > ^ ] [符号：+] [前缀：#] [宽度] [精度]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'4 1 3 2'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字段名 注：关键字参数放在最后\n",
    "\"{foo} {} {bar} {}\".format(1,2,foo=4,bar=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'4 2 3 1'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字段名 注：关键字参数放在最后\n",
    "\"{foo} {1} {bar} {0}\".format(1,2,foo=4,bar=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "cannot switch from manual field specification to automatic field numbering",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-18-029ce2eac591>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# 字段名 注：关键字参数放在最后\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;34m\"{foo} {1} {bar} {}\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mfoo\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mbar\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m: cannot switch from manual field specification to automatic field numbering"
     ]
    }
   ],
   "source": [
    "# 字段名 注：关键字参数放在最后\n",
    "# 不能同时使用手工编号和自动编号\n",
    "\"{foo} {1} {bar} {}\".format(1,2,foo=4,bar=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Mr Smoketiimuc'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fullname = ['Alfred','Smoketiimuc']\n",
    "\"Mr {name[1]}\".format(name = fullname)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'the math module defines the value 3.141592653589793 for pai'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import math\n",
    "tmpl = \"the {mod.__name__} module defines the value {mod.pi} for pai\"\n",
    "tmpl.format(mod=math)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "p 'p' 'p'\n"
     ]
    }
   ],
   "source": [
    "# 基本转换\n",
    "print(\"{pi!s} {pi!r} {pi!a}\".format(pi='p'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'the nums is 42.000000'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 指定类型  浮点型默认6位小数\n",
    "\"the nums is {num:f}\".format(num=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'the nums is 101010'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 指定类型  二进制\n",
    "\"the nums is {num:b}\".format(num=42)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字符串格式化类型说明"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### b    将整数转为二进制\n",
    "#### c    将整数转为unicode\n",
    "#### d    将整数视为十进制\n",
    "#### e    科学计数\n",
    "#### E    与e相同  但是使用E来表示指数\n",
    "#### f    浮点数\n",
    "#### F    与f相同 但对特殊值（nan，inf），使用大写表示\n",
    "#### g    自动选择浮点和科学计算 默认一位小数\n",
    "#### G    与g相同 但使用大写来表示指数和特殊值\n",
    "#### n    与g相同 但插入随区域而异的数字分隔符\n",
    "#### o    八进制\n",
    "#### s    字符串\n",
    "#### x    十六进制\n",
    "#### X    十六进制，大写\n",
    "#### %    百分比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'         3'"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 宽度 数字默认右对齐\n",
    "'{num:10}'.format(num=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'      3.00'"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 宽度 精度 \n",
    "'{num:10.2f}'.format(num=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'bob       '"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字符串 默认左对齐\n",
    "\"{name:10}\".format(name='bob')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'bo        '"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字符串 精度 \n",
    "\"{name:10.2}\".format(name='bob')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'10,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000'"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 千位分隔符\n",
    "\"{:,}\".format(10**100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.14$$$$$$ \n",
      " $$$3.14$$$ \n",
      " $$$$$$3.14\n"
     ]
    }
   ],
   "source": [
    "# 符号 对齐 和填充\n",
    "\n",
    "print(\"{0:$<10.2f} \\n {0:$^10.2f} \\n {0:$>10.2f}\".format(pi))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "+     3.14\n",
      "-     3.14\n"
     ]
    }
   ],
   "source": [
    "# = 分隔符号和数字\n",
    "from math import pi\n",
    "print(\"{0:=+10.2f}\\n{1:=+10.2f}\".format(pi,-pi))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "please enter width: 20\n",
      "====================\n",
      "Item           Price\n",
      "--------------------\n",
      "Apples          0.40\n",
      "Pears           0.50\n",
      "====================\n"
     ]
    }
   ],
   "source": [
    "# 实例\n",
    "\n",
    "width = int(input('please enter width: '))\n",
    "\n",
    "price_width = 10\n",
    "item_width = width - price_width\n",
    "\n",
    "header_fmt = \"{{:<{}}}{{:>{}}}\".format(item_width,price_width)\n",
    "fmt = \"{{:<{}}}{{:>{}.2f}}\".format(item_width,price_width)\n",
    "\n",
    "print('='*width)\n",
    "print(header_fmt.format('Item','Price'))\n",
    "print('-'*width)\n",
    "print(fmt.format('Apples',0.4))\n",
    "print(fmt.format('Pears',0.5))\n",
    "print('='*width)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'$$$$$$$$$$$$$$$$$mr.big$$$$$$$$$$$$$$$$$'"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# center 在两边填充\n",
    "'mr.big'.center(40,'$')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# find 查找 返回索引 否则 -1\n",
    "# fing(字符，开始，结束)\n",
    "'Mr.big'.find('b',1,7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1+2+3+4'"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# join 与split相反  用于合并元素\n",
    "seq = ['1','2','3','4']\n",
    "sep = '+'\n",
    "sep.join(seq)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'trondheim hanmmer dance'"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# lower \n",
    "\"Trondheim Hanmmer Dance\".lower()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Trondheim Hanmmer Dance'"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# title\n",
    "'trondheim hanmmer dance'.title()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'aSdfaSdfaSdf'"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# string.capwords(s[,sep]) 用sep拆分后 首字母大写 在用sep合并\n",
    "import string\n",
    "string.capwords('asdfasdfasdf',sep='a')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'thez ez a test'"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# replace (原 ， 新)\n",
    "'this is a test'.replace('is','ez')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['1', '2', '3', '4']"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# split(分隔符号)\n",
    "'1+2+3+4'.split('+')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'sadfas  df'"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# scrip 去掉两端空白\n",
    "'    sadfas  df   '.strip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{99: 107, 115: 122}"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# translate \n",
    "table = str.maketrans('cs', 'kz')\n",
    "table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'thiz iz an inkredible tezt'"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'this is an incredible test'.translate(table)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# isspace 判断是否空白\n",
    "# isdigit 判断是否数字\n",
    "# issupper 判断是否大写"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第四章 字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'Gumby', 'age': 24}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字典由键及值组成，这种键值对称为项（item）。\n",
    "# 创建字典 dict\n",
    "items = [('name','Gumby'),('age',24)]\n",
    "d = dict(items)\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'Gumby', 'age': 24}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 还可以使用关键字创建\n",
    "d = dict(name='Gumby',age=24)\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字典的基本操作\n",
    "# len（d）\n",
    "# d[k]\n",
    "# d[k] = v\n",
    "# del d[k]\n",
    "# k in d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name: Alice\n",
      "phone number(p) or address(a)?p\n",
      "Alice's phone number is 3241\n"
     ]
    }
   ],
   "source": [
    "# 实例\n",
    "people = {\n",
    "    \n",
    "    'Alice':{\n",
    "        'phone':'3241',\n",
    "        'addr':'foo drive 23'\n",
    "    },\n",
    "    'Beth':{\n",
    "        'phone':'9012',\n",
    "        'addr':'Bar street 42'\n",
    "    },\n",
    "    'Cecli':{\n",
    "        'phone':'3158',\n",
    "        'addr':'Baz avenue 90'\n",
    "    }\n",
    "}\n",
    "\n",
    "labels = {\n",
    "    'phone':'phone number',\n",
    "    'addr':'address'\n",
    "}\n",
    "\n",
    "name = input('Name: ')\n",
    "\n",
    "request = input('phone number(p) or address(a)?')\n",
    "\n",
    "if request == 'p': key = 'phone'\n",
    "if request == 'a': key = 'addr'\n",
    "    \n",
    "if name in people: print(\"{}'s {} is {}\".format(name,labels[key],people[name][key]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"Cecil's phone number is 3258\""
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# fromat_map 使用字典格式化字符串\n",
    "phonebook = {'Beth':'9102','Alice':'2341','Cecil':'3258'}\n",
    "\"Cecil's phone number is {Cecil}\".format_map(phonebook)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"Cecil's phone number is 3258\""
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# fromat使用**解包字典格式化字符串\n",
    "phonebook = {'Beth':'9102','Alice':'2341','Cecil':'3258'}\n",
    "\"Cecil's phone number is {Cecil}\".format(**phonebook)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "<html>\n",
      "<head><title>My Home Page</title></head>\n",
      "<body>\n",
      "<h1>My Home Page</h1>\n",
      "\n",
      "<p>Welcome to my home page!</p>\n",
      "</body>\n",
      "</html>\n",
      "\n"
     ]
    }
   ],
   "source": [
    "template = '''\n",
    "<html>\n",
    "<head><title>{title}</title></head>\n",
    "<body>\n",
    "<h1>{title}</h1>\n",
    "\n",
    "<p>{text}</p>\n",
    "</body>\n",
    "</html>\n",
    "'''\n",
    "\n",
    "data = {'title':'My Home Page','text':'Welcome to my home page!'}\n",
    "\n",
    "print(template.format_map(data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字典的方法\n",
    "d.clear() #删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'username': 'mlh', 'machines': ['foo', 'baz']}\n",
      "{'username': 'admin', 'machines': ['foo', 'baz']}\n"
     ]
    }
   ],
   "source": [
    "# 浅复制 copy 注意 列表被改变了\n",
    "x = {'username':'admin','machines':['foo','bar','baz']}\n",
    "y = x.copy()\n",
    "y['username'] = 'mlh'\n",
    "y['machines'].remove('bar')  #浅拷贝会改变\n",
    "print(y)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'names': ['Alfred', 'Bertrand', 'Clive']}\n",
      "{'names': ['Alfred', 'Bertrand', 'Clive']}\n",
      "{'names': ['Alfred', 'Bertrand']}\n"
     ]
    }
   ],
   "source": [
    "# 深复制 eepcopy(d)\n",
    "from copy import deepcopy\n",
    "d = {}\n",
    "d['names'] = ['Alfred','Bertrand']\n",
    "c = d.copy()\n",
    "dc = deepcopy(d)\n",
    "\n",
    "d['names'].append('Clive')\n",
    "\n",
    "print(d)\n",
    "print(c)\n",
    "print(dc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': None, 'age': None}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建字典  fromkeys\n",
    "{}.fromkeys(['name','age'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'unknow', 'age': 'unknow'}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict.fromkeys(['name','age'],'unknow')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w\n"
     ]
    }
   ],
   "source": [
    "# .get('键'，‘没有键时返回值，默认None’)\n",
    "print(d.get('name','w'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name: Alice\n",
      "phone number(p) or address(a)?5s\n",
      "Alice's 5s is not available\n"
     ]
    }
   ],
   "source": [
    "# 实例\n",
    "people = {\n",
    "    \n",
    "    'Alice':{\n",
    "        'phone':'3241',\n",
    "        'addr':'foo drive 23'\n",
    "    },\n",
    "    'Beth':{\n",
    "        'phone':'9012',\n",
    "        'addr':'Bar street 42'\n",
    "    },\n",
    "    'Cecli':{\n",
    "        'phone':'3158',\n",
    "        'addr':'Baz avenue 90'\n",
    "    }\n",
    "}\n",
    "\n",
    "labels = {\n",
    "    'phone':'phone number',\n",
    "    'addr':'address'\n",
    "}\n",
    "\n",
    "name = input('Name: ')\n",
    "\n",
    "request = input('phone number(p) or address(a)?')\n",
    "\n",
    "key = request\n",
    "if request == 'p': key = 'phone'\n",
    "if request == 'a': key = 'addr'\n",
    "\n",
    "person = people.get(name,{})\n",
    "label = labels.get(key,key)\n",
    "result = person.get(label,'not available')\n",
    "    \n",
    "    \n",
    "if name in people: print(\"{}'s {} is {}\".format(name,label,result))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([('title', 'python wei sei'), ('url', 'https://www.python.org')])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# d.items 返回字典所有项的列表。××××××始终随着字典而变化×××××。\n",
    "d = {'title':'python wei sei','url':'https://www.python.org'}\n",
    "d.items()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_items([('title', 'python wei sei'), ('url', 'https://www.python.org')])\n",
      "dict_items([('title', 'python wei sei'), ('url', 'https://www.python.org'), ('name', [1, 2, 3, 4])])\n"
     ]
    }
   ],
   "source": [
    "# 字典视图始终随着字典变化\n",
    "it = d.items()\n",
    "print(it)\n",
    "d['name'] = [1,2,3,4]\n",
    "print(it)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['title', 'url', 'name'])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 键的字典视图\n",
    "d.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pop 删除值\n",
    "d.pop('name')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'w'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-23-9511d16eed7d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0md\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'w'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m: 'w'"
     ]
    }
   ],
   "source": [
    "d.pop('w')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('url', 'https://www.python.org')"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# popitem 随机弹出一个字典项\n",
    "d.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'admin'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# d.setdefault(key, value)\n",
    "# 键不存在 返回value 并更新字典\n",
    "# 键存在 返回键对应的值\n",
    "d = {}\n",
    "d.setdefault('name','admin')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'admin'}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hahah'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d['name'] = 'hahah'\n",
    "d.setdefault('name','admin')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'hahah', 'age': 42}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# updata 用一个字典更新另一个字典\n",
    "nd = {'age':42}\n",
    "d.update(nd)\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values(['hahah', 42])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 值的字典视图\n",
    "d.values()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第五章 条件，循环及其他语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I_wish_to_register_a_complaint\n"
     ]
    }
   ],
   "source": [
    "#print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n",
    "print('I','wish','to','register','a','complaint',sep='_')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3\n"
     ]
    }
   ],
   "source": [
    "#序列解包\n",
    "x,y,z = 1,2,3\n",
    "print(x,y,z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "not enough values to unpack (expected 3, got 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-23-e0285e8d049f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0my\u001b[0m \u001b[0;34m,\u001b[0m\u001b[0mz\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m: not enough values to unpack (expected 3, got 2)"
     ]
    }
   ],
   "source": [
    "x,y ,z = 1,2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "too many values to unpack (expected 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-24-be1ed55d97f6>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0my\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m: too many values to unpack (expected 2)"
     ]
    }
   ],
   "source": [
    "x,y = 1,2,3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 [3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "#收集参数\n",
    "x,y,*z = 1,2,3,4,5\n",
    "print(x,y,z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 [] 2\n"
     ]
    }
   ],
   "source": [
    "#收集参数 收集 剩下的参数 没有就返回空\n",
    "a,*b,c = 1,2\n",
    "print(a,b,c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Albus ['Percival', 'Wulfric', 'Brian'] Dumbledore\n"
     ]
    }
   ],
   "source": [
    "#收集参数\n",
    "name= 'Albus Percival Wulfric Brian Dumbledore'\n",
    "a,*b,c = name.split()\n",
    "print(a,b,c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 1\n"
     ]
    }
   ],
   "source": [
    "x,y = y,x\n",
    "print(x,y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "girlfriend Marion\n"
     ]
    }
   ],
   "source": [
    "scoundrel = {'name':'Robin','girlfriend':'Marion'}\n",
    "key, value = scoundrel.popitem()\n",
    "print(key,value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "#链式赋值\n",
    "x=y=lambda x:x+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x is y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x == y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "#第二种\n",
    "\n",
    "def fuc():\n",
    "    pass\n",
    "\n",
    "x = fuc()\n",
    "y = fuc()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x is y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x == y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#增强赋值\n",
    "x = 1\n",
    "x+=1\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "44"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "43 + True + False "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool('t')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''不要将 is  用于 数字 和字符串'''\n",
    "'a' is 'a'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''不要将 is  用于 数字 和字符串'''\n",
    "1 is 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "109"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "ord('m')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'m'"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chr(109)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#比较第一个\n",
    "[1,2] < [2,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#第一个相同  比较下一个\n",
    "[2,[1,2]] < [2,[1,3]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "ename": "AssertionError",
     "evalue": "the age must be realisstic",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAssertionError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-53-a957f1c6e649>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m#断言\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0mage\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m<\u001b[0m \u001b[0mage\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'the age must be realisstic'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mAssertionError\u001b[0m: the age must be realisstic"
     ]
    }
   ],
   "source": [
    "#断言\n",
    "age = -1\n",
    "assert 0< age < 10,'the age must be realisstic'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Please enter your name:\n",
      "Please enter your name:\n",
      "Please enter your name:'w'\n",
      "Hello, 'w'!\n"
     ]
    }
   ],
   "source": [
    "#while 循环\n",
    "name = ''\n",
    "while not name:\n",
    "    name = input('Please enter your name:')\n",
    "print('Hello, {}!'.format(name))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this\n",
      "is\n",
      "an\n",
      "ex\n",
      "parrot\n"
     ]
    }
   ],
   "source": [
    "#for 循环\n",
    "words = ['this','is','an','ex','parrot']\n",
    "for word in words:\n",
    "    print(word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x corresponds to 1\n",
      "y corresponds to 2\n",
      "z corresponds to 3\n"
     ]
    }
   ],
   "source": [
    "d={'x':1,'y':2,'z':3}\n",
    "for key,value in d.items():\n",
    "    print(key,'corresponds to', value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "anne 12\n",
      "beth 45\n",
      "george 32\n",
      "damon 102\n"
     ]
    }
   ],
   "source": [
    "#并行迭代 长度以短的为准\n",
    "names = ['anne','beth','george','damon']\n",
    "ages = [12,45,32,102,88,99]\n",
    "for name,age in zip(names,ages):\n",
    "    print(name,age)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on class enumerate in module builtins:\n",
      "\n",
      "class enumerate(object)\n",
      " |  enumerate(iterable, start=0)\n",
      " |  \n",
      " |  Return an enumerate object.\n",
      " |  \n",
      " |    iterable\n",
      " |      an object supporting iteration\n",
      " |  \n",
      " |  The enumerate object yields pairs containing a count (from start, which\n",
      " |  defaults to zero) and a value yielded by the iterable argument.\n",
      " |  \n",
      " |  enumerate is useful for obtaining an indexed list:\n",
      " |      (0, seq[0]), (1, seq[1]), (2, seq[2]), ...\n",
      " |  \n",
      " |  Methods defined here:\n",
      " |  \n",
      " |  __getattribute__(self, name, /)\n",
      " |      Return getattr(self, name).\n",
      " |  \n",
      " |  __iter__(self, /)\n",
      " |      Implement iter(self).\n",
      " |  \n",
      " |  __next__(self, /)\n",
      " |      Implement next(self).\n",
      " |  \n",
      " |  __reduce__(...)\n",
      " |      Return state information for pickling.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Static methods defined here:\n",
      " |  \n",
      " |  __new__(*args, **kwargs) from builtins.type\n",
      " |      Create and return a new object.  See help(type) for accurate signature.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(enumerate)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 a\n",
      "1 b\n",
      "2 c\n",
      "3 d\n",
      "4 e\n",
      "5 f\n"
     ]
    }
   ],
   "source": [
    "#enumerate 枚举\n",
    "strings = ['a','b','c','d','e','f']\n",
    "for index , string in enumerate(strings):\n",
    "    print(index,string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['e', 'h', 'z', 'a', 'm']"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(reversed('mazhe'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'h', 'm', 'z']"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(sorted('mazhe'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name:5\n",
      "5\n",
      "name:'a'\n",
      "'a'\n",
      "name:\n"
     ]
    }
   ],
   "source": [
    "#break 跳出当前循环\n",
    "while True:\n",
    "    name = input('name:')\n",
    "    if not name:break\n",
    "    print(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "did not find it\n"
     ]
    }
   ],
   "source": [
    "'''else 与 if 可以不对其'''\n",
    "from math import sqrt\n",
    "\n",
    "for n in range(99,81,-1):\n",
    "    root = sqrt(n)\n",
    "    if root == int(root):\n",
    "        print(n)\n",
    "        break\n",
    "else:\n",
    "    print('did not find it')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "did not find it\n",
      "did not find it\n",
      "did not find it\n",
      "did not find it\n",
      "did not find it\n",
      "did not find it\n",
      "did not find it\n",
      "did not find it\n",
      "did not find it\n",
      "did not find it\n",
      "did not find it\n",
      "did not find it\n",
      "did not find it\n",
      "did not find it\n",
      "did not find it\n",
      "did not find it\n",
      "did not find it\n",
      "did not find it\n"
     ]
    }
   ],
   "source": [
    "from math import sqrt\n",
    "\n",
    "for n in range(99,81,-1):\n",
    "    root = sqrt(n)\n",
    "    if root == int(root):\n",
    "        print(n)\n",
    "        break\n",
    "    else:\n",
    "        print('did not find it')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "did not find it\n"
     ]
    }
   ],
   "source": [
    "#else 可以单独出现\n",
    "from math import sqrt\n",
    "\n",
    "for n in range(99,81,-1):\n",
    "    root = sqrt(n)\n",
    "else:\n",
    "    print('did not find it')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w\n"
     ]
    }
   ],
   "source": [
    "#else 可以单独出现for后\n",
    "for k in range(3):\n",
    "    pass\n",
    "else:\n",
    "    print('w')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(0, 0),\n",
       " (0, 2),\n",
       " (0, 4),\n",
       " (0, 6),\n",
       " (0, 8),\n",
       " (3, 0),\n",
       " (3, 2),\n",
       " (3, 4),\n",
       " (3, 6),\n",
       " (3, 8),\n",
       " (6, 0),\n",
       " (6, 2),\n",
       " (6, 4),\n",
       " (6, 6),\n",
       " (6, 8),\n",
       " (9, 0),\n",
       " (9, 2),\n",
       " (9, 4),\n",
       " (9, 6),\n",
       " (9, 8)]"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#分别计算x,y 然后交叉组合\n",
    "[(x,y) for x in range(10) if x%3==0 for y in range(10) if y%2==0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['chris+clarice', 'arnold+alice', 'bob+bernice']"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#首字母相同配对\n",
    "boys = ['chris','arnold','bob']\n",
    "girls = ['alice','bernice','clarice']\n",
    "[boy+'+'+girl for boy in boys for girl in girls if boy[0]==girl[0]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['chris+clarice', 'arnold+alice', 'bob+bernice']"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#首字母相同配对  boys多一个不会报错\n",
    "boys = ['chris','arnold','bob','device']\n",
    "girls = ['alice','bernice','clarice']\n",
    "[boy+'+'+girl for boy in boys for girl in girls if boy[0]==girl[0]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['chris+clarice', 'arnold+alice', 'bob+bernice']\n"
     ]
    }
   ],
   "source": [
    "#首字母相同配对2\n",
    "boys = ['chris','arnold','bob']\n",
    "girls = ['alice','bernice','clarice']\n",
    "letterGirls = {}\n",
    "for girl in girls:\n",
    "    letterGirls.setdefault(girl[0],[]).append(girl)\n",
    "\n",
    "print([boy + '+' + girl for boy in boys for girl in letterGirls[boy[0]]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'d'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-85-e961bcfd653b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      6\u001b[0m     \u001b[0mletterGirls\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msetdefault\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgirl\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgirl\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mboy\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m'+'\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mgirl\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mboy\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mboys\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mgirl\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mletterGirls\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mboy\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m<ipython-input-85-e961bcfd653b>\u001b[0m in \u001b[0;36m<listcomp>\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m      6\u001b[0m     \u001b[0mletterGirls\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msetdefault\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgirl\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgirl\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mboy\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m'+'\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mgirl\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mboy\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mboys\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mgirl\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mletterGirls\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mboy\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m: 'd'"
     ]
    }
   ],
   "source": [
    "#首字母相同配对2 多一个 会报错\n",
    "boys = ['chris','arnold','bob','device']\n",
    "girls = ['alice','bernice','clarice']\n",
    "letterGirls = {}\n",
    "for girl in girls:\n",
    "    letterGirls.setdefault(girl[0],[]).append(girl)\n",
    "\n",
    "print([boy + '+' + girl for boy in boys for girl in letterGirls[boy[0]]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "boss\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "#exec eval 执行语句  eval 返回值\n",
    "w = exec('print(\"boss\")')\n",
    "print(w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "boss\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "w = eval('print(\"boss\")')\n",
    "print(w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "boss name:nn\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "w = exec('input(\"boss name:\")')\n",
    "print(w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "boss name:55\n",
      "55\n"
     ]
    }
   ],
   "source": [
    "w = eval('input(\"boss name:\")')\n",
    "print(w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'int' object is not callable",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-14-eee2e3d469ae>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0mexec\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'sqrt=1'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0msqrt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: 'int' object is not callable"
     ]
    }
   ],
   "source": [
    "from math import sqrt\n",
    "\n",
    "exec('sqrt=1')\n",
    "sqrt(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['__builtins__', 'sqrt'])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#保险的做法是为 exec eval 提供一个映射作为命名空间\n",
    "from math import sqrt\n",
    "l = {}\n",
    "exec('sqrt=1',l)\n",
    "sqrt(2)\n",
    "l.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'x': 1,\n",
       " 'y': 2,\n",
       " '__builtins__': {'__name__': 'builtins',\n",
       "  '__doc__': \"Built-in functions, exceptions, and other objects.\\n\\nNoteworthy: None is the `nil' object; Ellipsis represents `...' in slices.\",\n",
       "  '__package__': '',\n",
       "  '__loader__': _frozen_importlib.BuiltinImporter,\n",
       "  '__spec__': ModuleSpec(name='builtins', loader=<class '_frozen_importlib.BuiltinImporter'>),\n",
       "  '__build_class__': <function __build_class__>,\n",
       "  '__import__': <function __import__>,\n",
       "  'abs': <function abs(x, /)>,\n",
       "  'all': <function all(iterable, /)>,\n",
       "  'any': <function any(iterable, /)>,\n",
       "  'ascii': <function ascii(obj, /)>,\n",
       "  'bin': <function bin(number, /)>,\n",
       "  'breakpoint': <function breakpoint>,\n",
       "  'callable': <function callable(obj, /)>,\n",
       "  'chr': <function chr(i, /)>,\n",
       "  'compile': <function compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1)>,\n",
       "  'delattr': <function delattr(obj, name, /)>,\n",
       "  'dir': <function dir>,\n",
       "  'divmod': <function divmod(x, y, /)>,\n",
       "  'eval': <function eval(source, globals=None, locals=None, /)>,\n",
       "  'exec': <function exec(source, globals=None, locals=None, /)>,\n",
       "  'format': <function format(value, format_spec='', /)>,\n",
       "  'getattr': <function getattr>,\n",
       "  'globals': <function globals()>,\n",
       "  'hasattr': <function hasattr(obj, name, /)>,\n",
       "  'hash': <function hash(obj, /)>,\n",
       "  'hex': <function hex(number, /)>,\n",
       "  'id': <function id(obj, /)>,\n",
       "  'input': <bound method Kernel.raw_input of <ipykernel.ipkernel.IPythonKernel object at 0xb61cbcac>>,\n",
       "  'isinstance': <function isinstance(obj, class_or_tuple, /)>,\n",
       "  'issubclass': <function issubclass(cls, class_or_tuple, /)>,\n",
       "  'iter': <function iter>,\n",
       "  'len': <function len(obj, /)>,\n",
       "  'locals': <function locals()>,\n",
       "  'max': <function max>,\n",
       "  'min': <function min>,\n",
       "  'next': <function next>,\n",
       "  'oct': <function oct(number, /)>,\n",
       "  'ord': <function ord(c, /)>,\n",
       "  'pow': <function pow(x, y, z=None, /)>,\n",
       "  'print': <function print>,\n",
       "  'repr': <function repr(obj, /)>,\n",
       "  'round': <function round(number, ndigits=None)>,\n",
       "  'setattr': <function setattr(obj, name, value, /)>,\n",
       "  'sorted': <function sorted(iterable, /, *, key=None, reverse=False)>,\n",
       "  'sum': <function sum(iterable, start=0, /)>,\n",
       "  'vars': <function vars>,\n",
       "  'None': None,\n",
       "  'Ellipsis': Ellipsis,\n",
       "  'NotImplemented': NotImplemented,\n",
       "  'False': False,\n",
       "  'True': True,\n",
       "  'bool': bool,\n",
       "  'memoryview': memoryview,\n",
       "  'bytearray': bytearray,\n",
       "  'bytes': bytes,\n",
       "  'classmethod': classmethod,\n",
       "  'complex': complex,\n",
       "  'dict': dict,\n",
       "  'enumerate': enumerate,\n",
       "  'filter': filter,\n",
       "  'float': float,\n",
       "  'frozenset': frozenset,\n",
       "  'property': property,\n",
       "  'int': int,\n",
       "  'list': list,\n",
       "  'map': map,\n",
       "  'object': object,\n",
       "  'range': range,\n",
       "  'reversed': reversed,\n",
       "  'set': set,\n",
       "  'slice': slice,\n",
       "  'staticmethod': staticmethod,\n",
       "  'str': str,\n",
       "  'super': super,\n",
       "  'tuple': tuple,\n",
       "  'type': type,\n",
       "  'zip': zip,\n",
       "  '__debug__': True,\n",
       "  'BaseException': BaseException,\n",
       "  'Exception': Exception,\n",
       "  'TypeError': TypeError,\n",
       "  'StopAsyncIteration': StopAsyncIteration,\n",
       "  'StopIteration': StopIteration,\n",
       "  'GeneratorExit': GeneratorExit,\n",
       "  'SystemExit': SystemExit,\n",
       "  'KeyboardInterrupt': KeyboardInterrupt,\n",
       "  'ImportError': ImportError,\n",
       "  'ModuleNotFoundError': ModuleNotFoundError,\n",
       "  'OSError': OSError,\n",
       "  'EnvironmentError': OSError,\n",
       "  'IOError': OSError,\n",
       "  'EOFError': EOFError,\n",
       "  'RuntimeError': RuntimeError,\n",
       "  'RecursionError': RecursionError,\n",
       "  'NotImplementedError': NotImplementedError,\n",
       "  'NameError': NameError,\n",
       "  'UnboundLocalError': UnboundLocalError,\n",
       "  'AttributeError': AttributeError,\n",
       "  'SyntaxError': SyntaxError,\n",
       "  'IndentationError': IndentationError,\n",
       "  'TabError': TabError,\n",
       "  'LookupError': LookupError,\n",
       "  'IndexError': IndexError,\n",
       "  'KeyError': KeyError,\n",
       "  'ValueError': ValueError,\n",
       "  'UnicodeError': UnicodeError,\n",
       "  'UnicodeEncodeError': UnicodeEncodeError,\n",
       "  'UnicodeDecodeError': UnicodeDecodeError,\n",
       "  'UnicodeTranslateError': UnicodeTranslateError,\n",
       "  'AssertionError': AssertionError,\n",
       "  'ArithmeticError': ArithmeticError,\n",
       "  'FloatingPointError': FloatingPointError,\n",
       "  'OverflowError': OverflowError,\n",
       "  'ZeroDivisionError': ZeroDivisionError,\n",
       "  'SystemError': SystemError,\n",
       "  'ReferenceError': ReferenceError,\n",
       "  'MemoryError': MemoryError,\n",
       "  'BufferError': BufferError,\n",
       "  'Warning': Warning,\n",
       "  'UserWarning': UserWarning,\n",
       "  'DeprecationWarning': DeprecationWarning,\n",
       "  'PendingDeprecationWarning': PendingDeprecationWarning,\n",
       "  'SyntaxWarning': SyntaxWarning,\n",
       "  'RuntimeWarning': RuntimeWarning,\n",
       "  'FutureWarning': FutureWarning,\n",
       "  'ImportWarning': ImportWarning,\n",
       "  'UnicodeWarning': UnicodeWarning,\n",
       "  'BytesWarning': BytesWarning,\n",
       "  'ResourceWarning': ResourceWarning,\n",
       "  'ConnectionError': ConnectionError,\n",
       "  'BlockingIOError': BlockingIOError,\n",
       "  'BrokenPipeError': BrokenPipeError,\n",
       "  'ChildProcessError': ChildProcessError,\n",
       "  'ConnectionAbortedError': ConnectionAbortedError,\n",
       "  'ConnectionRefusedError': ConnectionRefusedError,\n",
       "  'ConnectionResetError': ConnectionResetError,\n",
       "  'FileExistsError': FileExistsError,\n",
       "  'FileNotFoundError': FileNotFoundError,\n",
       "  'IsADirectoryError': IsADirectoryError,\n",
       "  'NotADirectoryError': NotADirectoryError,\n",
       "  'InterruptedError': InterruptedError,\n",
       "  'PermissionError': PermissionError,\n",
       "  'ProcessLookupError': ProcessLookupError,\n",
       "  'TimeoutError': TimeoutError,\n",
       "  'open': <function io.open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)>,\n",
       "  'copyright': Copyright (c) 2001-2018 Python Software Foundation.\n",
       "  All Rights Reserved.\n",
       "  \n",
       "  Copyright (c) 2000 BeOpen.com.\n",
       "  All Rights Reserved.\n",
       "  \n",
       "  Copyright (c) 1995-2001 Corporation for National Research Initiatives.\n",
       "  All Rights Reserved.\n",
       "  \n",
       "  Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.\n",
       "  All Rights Reserved.,\n",
       "  'credits':     Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands\n",
       "      for supporting Python development.  See www.python.org for more information.,\n",
       "  'license': Type license() to see the full license text,\n",
       "  'help': Type help() for interactive help, or help(object) for help about object.,\n",
       "  '__IPYTHON__': True,\n",
       "  'display': <function IPython.core.display.display(*objs, include=None, exclude=None, metadata=None, transient=None, display_id=None, **kwargs)>,\n",
       "  'get_ipython': <bound method InteractiveShell.get_ipython of <ipykernel.zmqshell.ZMQInteractiveShell object at 0xb717672c>>}}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用命名空间\n",
    "scope = {}\n",
    "scope['x'] = 1\n",
    "scope['y'] = 2\n",
    "\n",
    "eval('x + y',scope)\n",
    "scope"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['x', 'y', '__builtins__'])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#字典作为命名空间后 添加了————builtins———— 关键字\n",
    "print(scope.keys())\n",
    "\n",
    "scope2 = {}\n",
    "scope2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第六章 抽象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'this is doc \\n    anohter line'"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#编写函数文档\n",
    "def foo(x):\n",
    "    '''this is doc \n",
    "    anohter line'''\n",
    "    return x*x\n",
    "foo.__doc__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 4, 5]\n",
      "[0, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "#函数修改参数 列表 或这字典\n",
    "def bar(x):\n",
    "    x = 0\n",
    "\n",
    "def foo(x):\n",
    "    x[0] = 0\n",
    "    \n",
    "l = [3,4,5]\n",
    "\n",
    "bar(l)\n",
    "print(l)\n",
    "foo(l)\n",
    "print(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'first': {'Maguns': ['Maguns Lie Hetland']},\n",
       " 'middle': {'Lie': ['Maguns Lie Hetland']},\n",
       " 'last': {'Hetland': ['Maguns Lie Hetland']}}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "storage = {}\n",
    "storage['first'] = {}\n",
    "storage['middle'] = {}\n",
    "storage['last'] = {}\n",
    "\n",
    "me = 'Maguns Lie Hetland'\n",
    "storage['first']['Maguns'] = [me]\n",
    "storage['middle']['Lie'] = [me]\n",
    "storage['last']['Hetland'] = [me]\n",
    "\n",
    "storage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def init(data):\n",
    "    data['first'] = {}\n",
    "    data['middle'] = {}\n",
    "    data['last'] = {}\n",
    "    \n",
    "def lookup(data, label, name):\n",
    "    return data[label].get(name)\n",
    "\n",
    "def store(data, full_name):\n",
    "    names = full_name.split()\n",
    "    if len(names) ==2: names.insert(1,'')\n",
    "    labels = 'first','middle','last'\n",
    "    \n",
    "    for label,name in zip(labels, names):\n",
    "        people = lookup(data, label, name)\n",
    "        if people:\n",
    "            people.append(full_name)\n",
    "        else:\n",
    "            data[label][name] = [full_name]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "MyNames = {}\n",
    "init(MyNames)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Magnus Lie Hetlans']\n"
     ]
    }
   ],
   "source": [
    "\n",
    "store(MyNames,'Magnus Lie Hetlans')\n",
    "w = lookup(MyNames,'middle','Lie')\n",
    "print(w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'first': {'Magnus': ['Magnus Lie Hetlans']},\n",
       " 'middle': {'Lie': ['Magnus Lie Hetlans']},\n",
       " 'last': {'Hetlans': ['Magnus Lie Hetlans']}}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "MyNames"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Magnus Lie Hetlans', 'Magnus Lie Hetlans']\n"
     ]
    }
   ],
   "source": [
    "#store函数不能判断是否重复\n",
    "store(MyNames,'Magnus Lie Hetlans')\n",
    "w = lookup(MyNames,'middle','Lie')\n",
    "print(w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "foo() missing 1 required keyword-only argument: 'z'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-28-221aa5a8e8bc>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      5\u001b[0m     \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mz\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mfoo\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: foo() missing 1 required keyword-only argument: 'z'"
     ]
    }
   ],
   "source": [
    "#收集参数可以放在任何位置，收集参数后面的参数要用关键字参数\n",
    "def foo(x,*y,z):\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)\n",
    "\n",
    "foo(1,2,3,4,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "(2, 3, 4, 5, 6, 7)\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "#收集参数可以放在任何位置，收集参数后面的参数要用关键字参数\n",
    "foo(1,2,3,4,5,6,7,z=8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "(2, 3, 4, 5, 6, 7)\n",
      "{'w': 12, 'k': 34}\n"
     ]
    }
   ],
   "source": [
    "#使用**收集关键字参数 传入的ke不用加双引号\n",
    "def bar(x,*y,**z):\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)\n",
    "bar(1,2,3,4,5,6,7,w=12,k=34)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#分配参数\n",
    "\n",
    "def add(x,y):\n",
    "    return x+y\n",
    "\n",
    "params = (1,2)\n",
    "\n",
    "add(*params)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wwww, mazhe!\n"
     ]
    }
   ],
   "source": [
    "#分配参数\n",
    "def hello(greeting='Hello',name='world'):\n",
    "    print('{}, {}!'.format(greeting,name))\n",
    "    \n",
    "\n",
    "params = {'name':'mazhe','greeting':'wwww'}\n",
    "\n",
    "hello(**params)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "#联系使用参数\n",
    "'''format_map 使用字典格式化字符串'''\n",
    "def story(**kwds):\n",
    "    return 'Once upon a time, there was a '\\\n",
    "            '{job} called {name}.'.format_map(kwds)\n",
    "\n",
    "def power(x, y, *others):\n",
    "    if others:\n",
    "        print('received redundant paramters:', others)\n",
    "    return pow(x,y)\n",
    "\n",
    "def interval(start, stop=None, step=1):\n",
    "    'Imitates range() for step >0'\n",
    "    if stop is None:\n",
    "        start,stop = 0, start\n",
    "    result = []\n",
    "    \n",
    "    i=start\n",
    "    while i<stop:\n",
    "        result.append(i)\n",
    "        i+=step\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Once upon a time, there was a king called gumby.\n"
     ]
    }
   ],
   "source": [
    "print(story(job='king',name='gumby'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Once upon a time, there was a enginger called sir mazhe.\n"
     ]
    }
   ],
   "source": [
    "print(story(name='sir mazhe',job='enginger'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "story() takes 0 positional arguments but 1 was given",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-10-7be2b14e0965>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0mparams\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0;34m'job'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m'techer'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'name'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m'guoxue'\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstory\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mparams\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: story() takes 0 positional arguments but 1 was given"
     ]
    }
   ],
   "source": [
    "#函数中设置了收集参数  这里需要进行非陪参数\n",
    "params={'job':'techer','name':'guoxue'}\n",
    "print(story(params))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Once upon a time, there was a techer called guoxue.\n"
     ]
    }
   ],
   "source": [
    "#函数中设置了收集参数  这里需要进行非陪参数\n",
    "params={'job':'techer','name':'guoxue'}\n",
    "print(story(**params))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "power(2,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "power(3,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "received redundant paramters: (5,)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "3125"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "params = (5,)*3\n",
    "power(*params)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "interval(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 4]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "interval(2,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 5, 8]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "interval(2,9,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "received redundant paramters: (5, 6)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "81"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "power(*interval(3,7))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'__name__': '__main__',\n",
       " '__doc__': 'format_map 使用字典格式化字符串',\n",
       " '__package__': None,\n",
       " '__loader__': None,\n",
       " '__spec__': None,\n",
       " '__builtin__': <module 'builtins' (built-in)>,\n",
       " '__builtins__': <module 'builtins' (built-in)>,\n",
       " '_ih': ['',\n",
       "  '#分配参数\\n\\ndef add(x,y):\\n    return x+y\\n\\nparams = (1,2)\\n\\nadd(*params)',\n",
       "  \"#分配参数\\ndef hello(greeting='Hello',name='world'):\\n    print('{}, {}!'.format(greeting,name))\\n    \\nhello\\n\\nparams = {'name':'mazhe','greeting':'wwww'}\\n\\nhello(**params)\",\n",
       "  \"#联系使用参数\\n\\ndef story(**kwds):\\n    return 'Once upon a time, there was a '            '{job} called {name}.'.format_map(kwds)\",\n",
       "  \"#联系使用参数\\n'''format_map 使用字典格式化字符串'''\\ndef story(**kwds):\\n    return 'Once upon a time, there was a '            '{job} called {name}.'.format_map(kwds)\\n\\ndef power(x, y, *others):\\n    if others:\\n        print('received redundant paramters:', others)\\n    return pow(x,y)\\n\\ndef interval(start, stop=None, step=1):\\n    'Imitates range() for step >0'\\n    if stop is None:\\n        start,stop = 0, start\\n    result = []\\n    \\n    i=start\\n    while i<stop:\\n        result.append(i)\\n        i+=step\\n    return result\",\n",
       "  \"#联系使用参数\\n'''format_map 使用字典格式化字符串'''\\ndef story(**kwds):\\n    return 'Once upon a time, there was a '            '{job} called {name}.'.format_map(kwds)\\n\\ndef power(x, y, *others):\\n    if others:\\n        print('received redundant paramters:', others)\\n    return pow(x,y)\\n\\ndef interval(start, stop=None, step=1):\\n    'Imitates range() for step >0'\\n    if stop is None:\\n        start,stop = 0, start\\n    result = []\\n    \\n    i=start\\n    while i<stop:\\n        result.append(i)\\n        i+=step\\n    return result\",\n",
       "  \"print(story(job='king',name='gumby'))\",\n",
       "  \"print(story(job='king',name='gumby'))\",\n",
       "  \"print(story(name='sir mazhe',job='enginger'))\",\n",
       "  \"print(story(name='sir mazhe',job='enginger'))\",\n",
       "  \"params={'job':'techer','name':'guoxue'}\\nprint(story(params))\",\n",
       "  \"params={'job':'techer','name':'guoxue'}\\nprint(story(**params))\",\n",
       "  'power(2,3)',\n",
       "  'power(3,2)',\n",
       "  'params = (5,)*3',\n",
       "  'params = (5,)*3\\nparams',\n",
       "  'params = (5,)*3\\npower(params)',\n",
       "  'params = (5,)*3\\npower(*params)',\n",
       "  'interval(10)',\n",
       "  'interval(2,5)',\n",
       "  'interval(2,9,3)',\n",
       "  'power(*interval(3,7))',\n",
       "  '#作用域\\nvars()'],\n",
       " '_oh': {1: 3,\n",
       "  12: 8,\n",
       "  13: 9,\n",
       "  15: (5, 5, 5),\n",
       "  17: 3125,\n",
       "  18: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],\n",
       "  19: [2, 3, 4],\n",
       "  20: [2, 5, 8],\n",
       "  21: 81},\n",
       " '_dh': ['/home/mrbig/book'],\n",
       " 'In': ['',\n",
       "  '#分配参数\\n\\ndef add(x,y):\\n    return x+y\\n\\nparams = (1,2)\\n\\nadd(*params)',\n",
       "  \"#分配参数\\ndef hello(greeting='Hello',name='world'):\\n    print('{}, {}!'.format(greeting,name))\\n    \\nhello\\n\\nparams = {'name':'mazhe','greeting':'wwww'}\\n\\nhello(**params)\",\n",
       "  \"#联系使用参数\\n\\ndef story(**kwds):\\n    return 'Once upon a time, there was a '            '{job} called {name}.'.format_map(kwds)\",\n",
       "  \"#联系使用参数\\n'''format_map 使用字典格式化字符串'''\\ndef story(**kwds):\\n    return 'Once upon a time, there was a '            '{job} called {name}.'.format_map(kwds)\\n\\ndef power(x, y, *others):\\n    if others:\\n        print('received redundant paramters:', others)\\n    return pow(x,y)\\n\\ndef interval(start, stop=None, step=1):\\n    'Imitates range() for step >0'\\n    if stop is None:\\n        start,stop = 0, start\\n    result = []\\n    \\n    i=start\\n    while i<stop:\\n        result.append(i)\\n        i+=step\\n    return result\",\n",
       "  \"#联系使用参数\\n'''format_map 使用字典格式化字符串'''\\ndef story(**kwds):\\n    return 'Once upon a time, there was a '            '{job} called {name}.'.format_map(kwds)\\n\\ndef power(x, y, *others):\\n    if others:\\n        print('received redundant paramters:', others)\\n    return pow(x,y)\\n\\ndef interval(start, stop=None, step=1):\\n    'Imitates range() for step >0'\\n    if stop is None:\\n        start,stop = 0, start\\n    result = []\\n    \\n    i=start\\n    while i<stop:\\n        result.append(i)\\n        i+=step\\n    return result\",\n",
       "  \"print(story(job='king',name='gumby'))\",\n",
       "  \"print(story(job='king',name='gumby'))\",\n",
       "  \"print(story(name='sir mazhe',job='enginger'))\",\n",
       "  \"print(story(name='sir mazhe',job='enginger'))\",\n",
       "  \"params={'job':'techer','name':'guoxue'}\\nprint(story(params))\",\n",
       "  \"params={'job':'techer','name':'guoxue'}\\nprint(story(**params))\",\n",
       "  'power(2,3)',\n",
       "  'power(3,2)',\n",
       "  'params = (5,)*3',\n",
       "  'params = (5,)*3\\nparams',\n",
       "  'params = (5,)*3\\npower(params)',\n",
       "  'params = (5,)*3\\npower(*params)',\n",
       "  'interval(10)',\n",
       "  'interval(2,5)',\n",
       "  'interval(2,9,3)',\n",
       "  'power(*interval(3,7))',\n",
       "  '#作用域\\nvars()'],\n",
       " 'Out': {1: 3,\n",
       "  12: 8,\n",
       "  13: 9,\n",
       "  15: (5, 5, 5),\n",
       "  17: 3125,\n",
       "  18: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],\n",
       "  19: [2, 3, 4],\n",
       "  20: [2, 5, 8],\n",
       "  21: 81},\n",
       " 'get_ipython': <bound method InteractiveShell.get_ipython of <ipykernel.zmqshell.ZMQInteractiveShell object at 0xb625064c>>,\n",
       " 'exit': <IPython.core.autocall.ZMQExitAutocall at 0xb6209cec>,\n",
       " 'quit': <IPython.core.autocall.ZMQExitAutocall at 0xb6209cec>,\n",
       " '_': 81,\n",
       " '__': [2, 5, 8],\n",
       " '___': [2, 3, 4],\n",
       " '_i': 'power(*interval(3,7))',\n",
       " '_ii': 'interval(2,9,3)',\n",
       " '_iii': 'interval(2,5)',\n",
       " '_i1': '#分配参数\\n\\ndef add(x,y):\\n    return x+y\\n\\nparams = (1,2)\\n\\nadd(*params)',\n",
       " 'add': <function __main__.add(x, y)>,\n",
       " 'params': (5, 5, 5),\n",
       " '_1': 3,\n",
       " '_i2': \"#分配参数\\ndef hello(greeting='Hello',name='world'):\\n    print('{}, {}!'.format(greeting,name))\\n    \\nhello\\n\\nparams = {'name':'mazhe','greeting':'wwww'}\\n\\nhello(**params)\",\n",
       " 'hello': <function __main__.hello(greeting='Hello', name='world')>,\n",
       " '_i3': \"#联系使用参数\\n\\ndef story(**kwds):\\n    return 'Once upon a time, there was a '\\\\\\n            '{job} called {name}.'.format_map(kwds)\",\n",
       " 'story': <function __main__.story(**kwds)>,\n",
       " '_i4': \"#联系使用参数\\n'''format_map 使用字典格式化字符串'''\\ndef story(**kwds):\\n    return 'Once upon a time, there was a '\\\\\\n            '{job} called {name}.'.format_map(kwds)\\n\\ndef power(x, y, *others):\\n    if others:\\n        print('received redundant paramters:', others)\\n    return pow(x,y)\\n\\ndef interval(start, stop=None, step=1):\\n    'Imitates range() for step >0'\\n    if stop is None:\\n        start,stop = 0, start\\n    result = []\\n    \\n    i=start\\n    while i<stop:\\n        result.append(i)\\n        i+=step\\n    return result\",\n",
       " 'power': <function __main__.power(x, y, *others)>,\n",
       " 'interval': <function __main__.interval(start, stop=None, step=1)>,\n",
       " '_i5': \"#联系使用参数\\n'''format_map 使用字典格式化字符串'''\\ndef story(**kwds):\\n    return 'Once upon a time, there was a '\\\\\\n            '{job} called {name}.'.format_map(kwds)\\n\\ndef power(x, y, *others):\\n    if others:\\n        print('received redundant paramters:', others)\\n    return pow(x,y)\\n\\ndef interval(start, stop=None, step=1):\\n    'Imitates range() for step >0'\\n    if stop is None:\\n        start,stop = 0, start\\n    result = []\\n    \\n    i=start\\n    while i<stop:\\n        result.append(i)\\n        i+=step\\n    return result\",\n",
       " '_i6': \"print(story(job='king',name='gumby'))\",\n",
       " '_i7': \"print(story(job='king',name='gumby'))\",\n",
       " '_i8': \"print(story(name='sir mazhe',job='enginger'))\",\n",
       " '_i9': \"print(story(name='sir mazhe',job='enginger'))\",\n",
       " '_i10': \"params={'job':'techer','name':'guoxue'}\\nprint(story(params))\",\n",
       " '_i11': \"params={'job':'techer','name':'guoxue'}\\nprint(story(**params))\",\n",
       " '_i12': 'power(2,3)',\n",
       " '_12': 8,\n",
       " '_i13': 'power(3,2)',\n",
       " '_13': 9,\n",
       " '_i14': 'params = (5,)*3',\n",
       " '_i15': 'params = (5,)*3\\nparams',\n",
       " '_15': (5, 5, 5),\n",
       " '_i16': 'params = (5,)*3\\npower(params)',\n",
       " '_i17': 'params = (5,)*3\\npower(*params)',\n",
       " '_17': 3125,\n",
       " '_i18': 'interval(10)',\n",
       " '_18': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],\n",
       " '_i19': 'interval(2,5)',\n",
       " '_19': [2, 3, 4],\n",
       " '_i20': 'interval(2,9,3)',\n",
       " '_20': [2, 5, 8],\n",
       " '_i21': 'power(*interval(3,7))',\n",
       " '_21': 81,\n",
       " '_i22': '#作用域\\nvars()'}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#作用域 \n",
    "vars()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "globals() == vars()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "shrubberry\n"
     ]
    }
   ],
   "source": [
    "'''使用 globals()  访问全局变量'''\n",
    "\n",
    "def combine(parameter):\n",
    "    print(parameter + globals()['paramter'])\n",
    "    \n",
    "paramter = 'berry'\n",
    "combine('shrub')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''闭包 嵌套函数 外部调用内部函数 使用外部函数变量'''\n",
    "def multiplier(factor):\n",
    "    def multiplierByFactor(number):\n",
    "        return number + factor\n",
    "    return multiplierByFactor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "362880"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''递归 必须有返回条件'''\n",
    "\n",
    "def factorian(n):\n",
    "    if n == 1:\n",
    "        return 1\n",
    "    else:\n",
    "        return n* factorian(n-1)\n",
    "factorian(9)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''递归 二分查找'''\n",
    "def search(sequence, number, lower=0, upper=None):\n",
    "    if upper is None:\n",
    "        upper = len(sequence) - 1\n",
    "    if lower == upper:\n",
    "        assert number == sequence[upper]\n",
    "        return upper\n",
    "    else:\n",
    "        middle = (lower + upper) //2\n",
    "        if number > sequence[middle]:\n",
    "            return search(sequence, number, middle+1, upper)\n",
    "        else:\n",
    "            return search(sequence, number, lower, middle)\n",
    "seq = [1,3,6,8,9,34,68]\n",
    "\n",
    "search(seq,34)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''map:函数遍历 filter：根据函数返回值过滤 reduce上一个返回值加入到下一次函数'''"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第七章 再谈抽象 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Person():\n",
    "    \n",
    "    def set_name(self, name):\n",
    "        self.name = name\n",
    "    def get_name(self):\n",
    "        return self.name\n",
    "    def greet(self):\n",
    "        print('Hello, world! I am {}'.format(self.name))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, world! I am foo\n"
     ]
    }
   ],
   "source": [
    "# set_name 有没有都一样\n",
    "foo = Person()\n",
    "foo.name = 'foo'\n",
    "foo.greet()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "#私有属性 外部方位方法\n",
    "\n",
    "class Secretive:\n",
    "    def __inaccessible(self):\n",
    "        print('bet you can not see me')\n",
    "    def accessible(self):\n",
    "        print('the secret message is:')\n",
    "        self.__inaccessible()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'Secretive' object has no attribute '__inaccessible'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-37-7ee1805b2fc0>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0ms\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mSecretive\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0ms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__inaccessible\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m: 'Secretive' object has no attribute '__inaccessible'"
     ]
    }
   ],
   "source": [
    "s = Secretive()\n",
    "s.__inaccessible()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the secret message is:\n",
      "bet you can not see me\n"
     ]
    }
   ],
   "source": [
    "s.accessible()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bet you can not see me\n"
     ]
    }
   ],
   "source": [
    "'''#私有属性 外部方位方法_类名__方法/属性名字'''\n",
    "s._Secretive__inaccessible()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''这里因该注意'''\n",
    "class MemberCounter:\n",
    "    memners =0\n",
    "    def init(self):\n",
    "        MemberCounter.memners+=1\n",
    "        #所有实例 均会指向同一个地址"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "m1 = MemberCounter()\n",
    "m1.init()\n",
    "print(m1.memners)\n",
    "print(MemberCounter.memners)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "m2 = MemberCounter()\n",
    "m2.init()\n",
    "print(m2.memners)\n",
    "print(MemberCounter.memners)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "m1.memners = 's' # 覆盖了原来的属性值\n",
    "print(m2.memners)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#继承\n",
    "class Filter:\n",
    "    def init(self):\n",
    "        self.blocked=[]\n",
    "    def filter(self, sequence):\n",
    "        return [x for x in sequence if x not in self.blocked]\n",
    "\n",
    "class SPAMFilter(Filter):\n",
    "    def init(self):\n",
    "        self.blocked=['SPAM']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = Filter()\n",
    "f.init()\n",
    "f.filter([1,2,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['eggs', 'bacon']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = SPAMFilter()\n",
    "s.init()\n",
    "s.filter(['SPAM','SPAM','SPAM','SPAM','SPAM','eggs','bacon','SPAM'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# issubclass 判断是否位子类\n",
    "issubclass(SPAMFilter,Filter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(__main__.Filter,)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#__bases__ 查询基类\n",
    "SPAMFilter.__bases__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#isinstance 判断是否位类的实例\n",
    "isinstance(s,SPAMFilter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#isinstance 判断是否位类的实例\n",
    "isinstance(s,Filter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "#多个超类\n",
    "'''前面的类覆盖后面的类的方法'''\n",
    "class Calculator:\n",
    "    def calculate(self,expression):\n",
    "        self.value = eval(expression)\n",
    "    def talk(self):\n",
    "        print('caover')\n",
    "class Talker:\n",
    "    def talk(self):\n",
    "        print('hi, my value is ', self.value)\n",
    "class TalkingCalculator(Calculator,Talker):\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "caover\n"
     ]
    }
   ],
   "source": [
    "tc = TalkingCalculator()\n",
    "tc.calculate('1+2*3')\n",
    "tc.talk()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(__main__.Calculator, __main__.Talker)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "TalkingCalculator.__bases__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#hasattr 验证实例是否具有属性\n",
    "hasattr(tc,'talk')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#callable 判断方法是否可以被调用\n",
    "\n",
    "callable(getattr(tc,'talk',None))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "callable(getattr(tc,'www',None))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "#setattr 为实例添加属性\n",
    "\n",
    "setattr(tc,'name','mazhe')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'mazhe'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tc.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'value': 7, 'name': 'mazhe'}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''查看实例的属性'''\n",
    "tc.__dict__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''在子类中必须实现的方法'''\n",
    "\n",
    "from abc import ABC, abstractmethod\n",
    "\n",
    "class Talker(ABC):\n",
    "    @abstractmethod\n",
    "    def talk(self):\n",
    "        pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "Can't instantiate abstract class Talker with abstract methods talk",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-15-29e18f7df7f3>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mTalker\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: Can't instantiate abstract class Talker with abstract methods talk"
     ]
    }
   ],
   "source": [
    "Talker()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "__main__.Herring"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#obj.register(obj2) 将obj2注册为obj\n",
    "#所有的obj2子类 都会被认定位 obj的子类\n",
    "class Herring:\n",
    "    def talk(self):\n",
    "        print('mazhe')\n",
    "Talker.register(Herring)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "总结\n",
    "\n",
    "将相关的东西放到一起。\n",
    "不要让对象之间过于亲密。\n",
    "慎用继承，尤其是多重继承。\n",
    "保持简单。\n",
    "----------------\n",
    "将有关的问题描述记下来，名词，动词，形容词\n",
    "名字--》类\n",
    "动子--》方法\n",
    "形容词--》属性\n",
    "'''"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第八章 异常 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
